﻿using Engine.Helpers;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.IO;

namespace UnitTestsScenarioEditor
{
    [TestClass]
    public class ZipArchiveTest
    {
        #region Private Fields

        private static ZipArchive zipFile;

        #endregion Private Fields

        #region Public Methods

        [ClassInitialize()]
        public static void InitZipArchiveTest(TestContext context)
        {
            ZipArchiveTest.zipFile = new ZipArchive();
        }

        /// <summary>
        /// In this Test a new TextFile added to the zip-archive, saved to file. Later the created zip-file is
        /// loaded and the textfile will be extracted and the text will be compared with new original text.
        /// </summary>
        [TestMethod]
        public void AddFileTest()
        {
            string zipFilename = "Test.newExtension";
            string filename = "Folder1/folder 2/Filename.txt";
            string inputString = "Hallo Test";

            using (MemoryStream ms = new MemoryStream())
            using (StreamWriter sr = new StreamWriter(ms))
            {
                //BinaryWriter bw = new BinaryWriter(sr.BaseStream);

                sr.WriteLine(inputString);
                sr.Flush();
                sr.BaseStream.Position = 0;
                //bw.Write("Hsllo");
                ZipArchiveTest.zipFile.AddData(filename, sr.BaseStream, true);
            }
            ZipArchiveTest.zipFile.SaveToFile(zipFilename);

            ZipArchive zipArchive = ZipArchive.FromFile(zipFilename);

            StreamReader sr2 = new StreamReader(zipArchive.GetStream(filename));

            string readString = sr2.ReadLine();

            Assert.AreEqual(inputString, readString);
        }

        #endregion Public Methods
    }
}